<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 <%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>

		<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
		<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
		<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>

<script type="text/javascript" src="<%=basePath%>Js/jquery.js"></script>
<link rel="stylesheet" type="text/css" href="<%=basePath%>css/mycss/bootstrap.min.css" />
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript">

function refresh(){
	window.location.href = "queryRepertoryAll.do";
}

//通过商品名称查询商品
function selectRepertoryByName(){
	var repertoryName = $("#repertoryName").val();
	window.location.href = "selectRepertoryByName.do?repertoryName="+repertoryName;
}

//通过商品分类查询商品
function selectRepertoryByClassify(){
	var repertoryClassify = $("#repertoryClassify").val();
	window.location.href = "selectRepertoryByClassify.do?repertoryClassify="+repertoryClassify;
}

//入库模态框显示
function addRepertory(id,type){
	
	$.ajax({
		type:"post",
		url:"addRepertory.do",
		data:{
			"id":id,
		},
		success:function(result){
			stus = JSON.parse(result)
			for(var i = 0; i < stus.length; i++ ){
				var stu = "";
				stu +="<tr>";
				stu +="<td>商品名称</td>";
				stu +="<td>"+stus[i].product+"</td>";
				stu +="</tr>";
				stu +="<tr>";
				stu +="<td>商品分类</td>";
				stu +="<td>"+stus[i].classify+"</td>";
				stu +="</tr>";
				stu +="<tr>";
				stu +="<td>单价</td>";
				stu +="<td>"+stus[i].price+"</td>";
				stu +="</tr>";
				stu +="<tr>";
				stu +="<td>数量</td>";
				stu +="<td>"+stus[i].quantity+"</td>";
				stu +="</tr>";
				
				var stu2 = "";
				if(type == "1"){
				stu2 +="<tr><td><input type='text' class='form-control' id='addQuantity' placeholder='请输入入库数量'><input type='hidden' id='repId' value='"+stus[i].reserved1+"'></td>"
				stu2 +="<td><button type='button' class='btn btn-success' onclick='add()'>入库</button></td></tr>"
				$("#quantityTable").html(stu2);
				$("#repTable").html(stu);
				}else{
					stu2 +="<tr><td><input type='text' class='form-control' id='moveQuantity' placeholder='请输入入库数量'><input type='hidden' id='reId' value='"+stus[i].reserved1+"'></td>"
					stu2 +="<td><button type='button' class='btn btn-success' onclick='moveModal()'>出库</button></td></tr>"
					$("#quanTable").html(stu2);
					$("#reTable").html(stu);
				}	
			}
			
		},
		error:function(){
			alert("系统故障")
		}
	})	
}

//入库
function add(){
	var repId = $("#repId").val();
	var addQuantity = $("#addQuantity").val();
	
	if (!(/(^[0-9]*[1-9][0-9]*$)/.test(addQuantity))){
		alert("输入的不是正整数");
	}else{
		if (addQuantity == '' || addQuantity == undefined || addQuantity == null) {
			alert("你没有输入入库数量")
	        } else {
	            $.ajax({
	            	type:"post",
	            	url:"addReper.do",
	            	data:{
	            		"repId":repId,
	            		"addQuantity":addQuantity,
	            	},
	            	dataType:"text",
	            	success:function(result){
	            		if(result == "1"){
	            			alert("入库成功")
	            		}else{
	            			alert("入库失败")
	            		}
	            	},
	            	error:function(){
	            		alert("系统故障")
	            	}
	            
	            })
	        }
	}	
}

//出库
function moveModal(){
	
	var repId = $("#reId").val();
	var moveQuantity = $("#moveQuantity").val();
	
	if (!(/(^[0-9]*[1-9][0-9]*$)/.test(moveQuantity))){
		alert("输入的不是正整数");
	}else{
		if (moveQuantity == '' || moveQuantity == undefined || moveQuantity == null) {
			alert("你没有输入入库数量")
	        } else {
	            $.ajax({
	            	type:"post",
	            	url:"stockRemoval.do",
	            	data:{
	            		"repId":repId,
	            		"moveQuantity":moveQuantity,
	            	},
	            	dataType:"text",
	            	success:function(result){
	            		if(result == "1"){
	            			alert("出库成功")
	            		}else{
	            			alert("出库失败")
	            		}
	            	},
	            	error:function(){
	            		alert("系统故障")
	            	}
	            
	            })
	        }
	}
}

//修改库存信息模态框显示
function amendRepertory(id){
	
	$.ajax({
		type:"post",
		url:"selectRepertoryById.do",
		data:{
			"id":id,
		},
	success:function(result){
		stus = JSON.parse(result)
		
		for(var i = 0; i < stus.length; i++ ){
			var stu = "";
			stu +="<tr>";
			stu +="<td>商品名称</td>";
			stu +="<td><input type='text' name='repProduct' value="+stus[i].product+" id='repProduct' class='form-control'/><input type='hidden' id='reId' value='"+stus[i].reserved1+"'</td>";
			stu +="</tr>";
			stu +="<tr>";
			stu +="<td>商品分类</td>";
			stu +="<td><input type='text' name='repclassify' value="+stus[i].classify+" id='repclassify' class='form-control'/></td>";
			stu +="</tr>";
			stu +="<tr>";
			stu +="<td>单价</td>";
			stu +="<td><input type='text' name='repPrice' value="+stus[i].price+" id='repPrice' class='form-control'/></td>";
			stu +="</tr>";
			stu +="<tr>";
			stu +="<td>数量</td>";
			stu +="<td><input type='text' name='repQuantity' value="+stus[i].quantity+" id='repQuantity' class='form-control'  readonly='readonly'/></td>";
			stu +="</tr>";
			stu +="<tr>";
			stu +="<td>备注</td>";
			stu +="<td><input value="+stus[i].remark+" type='text' name='repRemark'  id='repRemark' class='form-control'/></td>";
			stu +="<tr>";
			$("#amendTable").html(stu);
			
		}
		
	},
	error:function(){
		
	}
	})
}

function alterRepertory() {
	
	var reId = $("#reId").val();
	var repProduct = $("#repProduct").val();
	var repclassify = $("#repclassify").val();
	var repPrice = $("#repPrice").val();
	var repRemark = $("#repRemark").val();
	
	$.ajax({
		type:"post",
		url:"alterRepertory.do",
		data:{
			"reId":reId,
			"repProduct":repProduct,
			"repclassify":repclassify,
			"repPrice":repPrice,
			"repRemark":repRemark,
		},
		dataType:"text",
		success:function(resule){
			
			if(resule == "1"){
				alert("修改成功")
			}else{
				alert("修改失败")
			}
			
		},
		error:function(){
			alert("系统故障")
		}
	
	})
	
}

</script>
<body>
<hr><h3 align="center">库存查询</h3><hr>
<br>
<!-- 条件查询部分 -->
<table >
<tr><td><input type="text" name="repertoryName" id="repertoryName" class="form-control" placeholder="商品名称" value=""></td>
<td>&nbsp;&nbsp;<button class="btn btn-primary" onclick="selectRepertoryByName()">查询</button>&nbsp;&nbsp; </td>
<td><input type="text" name="repertoryClassify" id="repertoryClassify" class="form-control" placeholder="请输入商品分类" value=""></td>
<td>&nbsp;&nbsp;<button class="btn btn-primary" onclick="selectRepertoryByClassify()">查询</button>&nbsp;&nbsp; </td>
<td>&nbsp;&nbsp;<button class="btn btn-success" onclick="refresh()">刷新</button>&nbsp;&nbsp; </td>
</tr>
</table>
<br>
<br>

<!-- 查询信息显示部分 -->
<table class="table table-hover">
<tr>
<th>商品名称</th><th>商品分类</th><th>单价</th><th>剩余商品数量</th><th>备注</th><th>操作</th>
</tr>
<c:forEach items="${pageInfo.list}" var="relist">
<tr>
<td>${relist.product}</td><td>${relist.classify}</td><td>${relist.price}</td><td>${relist.quantity}</td>
<td>${relist.remark}</td>
<td><a href="#" onclick="addRepertory('${relist.reserved1}','1')" data-toggle="modal" data-target="#myModal">入库</a>
&nbsp;&nbsp;<a href="#" onclick="addRepertory('${relist.reserved1}','2')" data-toggle="modal" data-target="#moveModal">出库</a>&nbsp;&nbsp;
<a href="#" onclick="amendRepertory('${relist.reserved1}')" data-toggle="modal" data-target="#amendRepertory" >修改信息</a>
</td>
</tr>
</c:forEach>
</table>

	<div id="page1">
	<c:if test="${!pageInfo.isFirstPage }">
					<a href="${pageContext.request.contextPath }/queryRepertoryAll.do">首页</a>
					<a href="${pageContext.request.contextPath }/queryRepertoryAll.do?pageNum=${pageInfo.prePage}">上一页</a>
				</c:if>
				<c:if test="${!pageInfo.isLastPage }">
				<a href="${pageContext.request.contextPath }/queryRepertoryAll.do?pageNum=${pageInfo.nextPage}">下一页</a>
				<a href="${pageContext.request.contextPath }/queryRepertoryAll.do?pageNum=${pageInfo.lastPage}">末页</a>
				</c:if>
				当前页：${pageInfo.pageNum }/${pageInfo.pages }.
	</div>
	
		<!-- 入库模态框（Modal） -->
		<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
							&times;
						</button>
						<h3 class="modal-title" id="myModalLabel" align="center">
							入库
						</h3>
					</div>
					<div class="modal-body">
					<table class="table table-bordered" id="repTable"></table>
							
					</div>
					<div class="modal-footer" id="quantity">
					<table class="table table-bordered" id="quantityTable"></table>
					
					</div>
				</div><!-- /.modal-content -->
			</div><!-- /.modal -->
		</div>
		
		<!-- 出库模态框（Modal） -->
		<div class="modal fade" id="moveModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
							&times;
						</button>
						<h3 class="modal-title" id="myModalLabel" align="center">
							出库
						</h3>
					</div>
					<div class="modal-body">
					<table class="table table-bordered" id="reTable"></table>
							
					</div>
					<div class="modal-footer" id="quantity">
					<table class="table table-bordered" id="quanTable"></table>
					
					</div>
				</div><!-- /.modal-content -->
			</div><!-- /.modal -->
		</div>
		
		<!-- 修改信息模态框（Modal） -->
		<div class="modal fade" id="amendRepertory" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
							&times;
						</button>
						<h3 class="modal-title" id="myModalLabel" align="center">
							商品信息修改
						</h3>
					</div>
					<div class="modal-body">
					<table class="table table-bordered" id="amendTable"></table>
							
					</div>
					<div class="modal-footer" id="quantity">
					<button onclick="alterRepertory()" type="button" class="btn btn-success">修改</button>
					
					</div>
				</div><!-- /.modal-content -->
			</div><!-- /.modal -->
		</div>

</body>
</html>